package com.qdlc.p2p.dal.model;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.dal.dto.ExperienceRule;

/**
 * 体验金规则model
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 上午10:56:45
 */
public class ExperienceRuleModel extends ExperienceRule {
	
	private Logger logger = Logger.getLogger(ExperienceRuleModel.class);
	
	/** 状态-未启用 */
	public static final byte EXPERIENCERULE_STATUS_DISABLE = 0;
	/** 状态-启用 */
	public static final byte EXPERIENCERULE_STATUS_ENABLE = 1;
	
	/** 当前页码 */
	private int page = 1;

	/** 每页数据条数 */
	private int rows = 5;
	
	/**
	 * 模块
	 */
	private String module ;
	
	private String searchName;

	/**
	 * 领取开始时间
	 */
	private String receiveStartTime;
	/**
	 * 领取结束时间
	 */
	private String receiveEndTime;
	/**
	 * 过期开始时间
	 */
	private String expiredStartTime;
	/**
	 * 过期结束时间
	 */
	private String expiredEndTime;
	
	public static ExperienceRuleModel instance(ExperienceRule experienceRule) {
		ExperienceRuleModel experienceRuleModel = new ExperienceRuleModel();
		BeanUtils.copyProperties(experienceRule, experienceRuleModel);
		return experienceRuleModel;
	}

	public ExperienceRule prototype() {
		ExperienceRule experienceRule = new ExperienceRule();
		BeanUtils.copyProperties(this, experienceRule);
		return experienceRule;
	}
	
	/*************************define for manage  start**********************************/
	/**
	 * 检查提交的数据格式
	 * 
	 * @return
	 */
	public int validModelData() {
		if (!ValidateUtil.isAmount(getAmount())) {
			throw new BusinessException(MessageUtil.getMessage("MF102004004"), 1);
		}
		if (getAmount() > 10000){
			throw new BusinessException(MessageUtil.getMessage("MF102004005"), 1);
		}
		if(!ValidateUtil.isInteger(getValidTime())){
			throw new BusinessException(MessageUtil.getMessage("MF102004006"), 1);
		}
		if(!ValidateUtil.isInteger(getQuota())){
			throw new BusinessException(MessageUtil.getMessage("MF102004007"), 1);
		}
		return -1;
	}
	
	/**
	 * 添加
	 * 根据ExperienceRuleModel获得ExperienceRule
	 * @return
	 * @throws Exception
	 */
	public ExperienceRule prototypeForAppend() throws Exception {
		ExperienceRule experienceRule = this.prototype();
		experienceRule.setStatus(ExperienceRuleModel.EXPERIENCERULE_STATUS_ENABLE);
		experienceRule.setAddTime(DateUtil.getNow());
		return experienceRule;
	}
	/*************************define for manage  end**********************************/
	
	/*************************define for core  start**********************************/
	/**
	 * 为发放体验券做好体验券规则校验
	 * @param id
	 * @return
	 */
	public boolean checkForGiveExperience(ExperienceRule experienceRule) throws Exception {
		if(experienceRule == null){
			logger.error("体验券规则不存在，体验券发放失败！");
			return false;
		}
		if(experienceRule.getStatus()==0){
			logger.error("体验券规则未启用，体验券发放失败！id:"+experienceRule.getId());
			return false;
		}
		if(experienceRule.getQuotaYes()+1 > experienceRule.getQuota()){
			logger.error("体验券名额已使用完，体验券发放失败！id:"+experienceRule.getId());
			return false;
		}
		return true;
	}
	
	/*************************define for core  end**********************************/
	
//	private ExperienceRuleService getExperienceRuleService() {
//		return null;
//	}
//
//	private void setExperienceRuleService(ExperienceRuleService experienceRuleService) {
////		this.experienceRuleService = experienceRuleService;
//	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getReceiveStartTime() {
		return receiveStartTime;
	}

	public void setReceiveStartTime(String receiveStartTime) {
		this.receiveStartTime = receiveStartTime;
	}

	public String getReceiveEndTime() {
		return receiveEndTime;
	}

	public void setReceiveEndTime(String receiveEndTime) {
		this.receiveEndTime = receiveEndTime;
	}

	public String getExpiredStartTime() {
		return expiredStartTime;
	}

	public void setExpiredStartTime(String expiredStartTime) {
		this.expiredStartTime = expiredStartTime;
	}

	public String getExpiredEndTime() {
		return expiredEndTime;
	}

	public void setExpiredEndTime(String expiredEndTime) {
		this.expiredEndTime = expiredEndTime;
	}

}
